<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/5/26
 * Time: 17:07
 */

namespace Home\Controller;


use Home\Logic\GoodsLogic;
use Home\Logic\NoticeLogic;
use Home\Logic\PayLogic;
use Home\Logic\UserLocationLogic;
use Think\Controller;
use Common\Utils\WeChatUtils;
class IndexController extends BaseController
{
    public function index(){
//        $this->baidu_point();

        //获取用户
        $user = $this->getUser();
        $selectCity = I('city');
        $goodsLogic = new GoodsLogic();
        $noticeLogic = new NoticeLogic();
        $flag = 0; //标致是否有商品
        //获取地理位置
        $json = $this->getCityLocation();
        if(!$json)
            $json = get_ip_lookup();
        session('city',$json);
        $city = $selectCity == '' ? $json['city']:$selectCity;
//        $city = $this->getCityLocation();
        if(!$city){
            $city = '厦门市';
        }
        $hotGoodsList = $goodsLogic->getHotGoodsList($city);
        $specGoods = $goodsLogic->getSpecGoods($city);
        if(!$hotGoodsList)
        {
            $flag = 1; //没有商品 给出提示跳转到福建省
        }
        $noticeList = $noticeLogic->getNoticeList(1,10);
        self::jsSDK();

        $this->assign('user', $user);
        $this->assign('adList',$this->adList(4));
        $this->assign('noticeList',$noticeList);
        $this->assign('flag', $flag);
        $this->assign('hotGoodsList',$hotGoodsList);
        $this->assign('page_str','home');
        $this->assign('specGoods', $specGoods);
        $this->assign('city', $city);
        $this->display();
    }

    public function adList($count){
        $adList = D('Ad')->where(array('enabled'=>1))->limit('0,'.$count)->select();
        return $adList;
    }

    public function baidu_point(){

        $ip = get_ip();

        $ip = '';
        $ak = 'PfTWfabdG0NUjil9xhWO6ZWG7VX5iZY6';
        $url = 'http://api.map.baidu.com/location/ip?ak='.$ak . '&ip='. $ip;

        $date = file_get_contents($url);
        $date = json_decode($date);

//        var_dump($date);


    }

    public function selectCity(){

        $this->display();

    }

    public function getCityLocation(){
        $user = $this->getUser();
        $location = D('UserLocation')->where(array('user_id'=>':user_id'))->bind(array(':user_id'=>$user['user_id']))->find();
        if(!$location) {
            $location = D('UserLocation')->where(array('openid' => ':openid'))->bind(array(':openid' => $user['openid']))->find();
        }
        if($location) {
       /*     $url = "http://api.map.baidu.com/geocoder/v2/?ak=IHDKmkKAofhZDlUG8Vq2iqFKgQzvDt5c&location=" . $location['latitude'] . "," . $location['longitude'] . "&output=json&coordtype=gcj02ll";
            $output = file_get_contents($url);
            $address = json_decode($output, true);
            $city_name = $address['result']['addressComponent']['city'];//获取城市名称
//            var_dump($address);
            $city_code = $address['result']['cityCode'];//获取城市代码id
            if(!empty($city_name)) {//获取到城市的时候返回true
                return $city_name;
            }*/
            $address = array(
                'city'    =>$location['city'],
                'location'=>$location['location']
            );
            return $address;
        }
        return false;
    }

    public function test($lola='25.031063,117.014015'){
//        &coordtype=gcj02ll
        $url = "http://api.map.baidu.com/geocoder/v2/?ak=xzBzEDKwX4j5uF2Rdy4qbZTu8Mcz3zKq&location='.$lola.'&output=json";
        $output = file_get_contents($url);
        $address = json_decode($output, true);
        $city_name = $address['result']['addressComponent']['city'];//获取城市名称
        $city_code = $address['result']['cityCode'];//获取城市代码id
        var_dump($address);
    }

    public function getAddressBylola($lola='25.031063,117.014015'){
        $ak = 'xzBzEDKwX4j5uF2Rdy4qbZTu8Mcz3zKq';
        $url = 'http://api.map.baidu.com/geocoder/v2/?ak='.$ak.'&location='.$lola.'&output=json';
        $date = file_get_contents($url);
        $date = json_decode($date);
        var_dump($date);
        return $date->result->formatted_address . $date->result->sematic_description;
    }

    public function location(){
        $logic = new UserLocationLogic();
        $result = $logic->getLocation('25.031063','117.014015');
        var_dump($result);
    }


/*    public function  testLocationQQ(){
        $logic = new UserLocationLogic();
        $logic->getLocationByQQ();

    }

    public function  testAddSales(){
        $logic = new PayLogic();
        $logic->addSalesSum(576);
    }*/
    public function  testSQL(){

     var_dump( M('agent_log')->where(array('user_id'=> 2638))->getField("sum(money)"));

    }


}